﻿#include "MagnifyTimerWidget.h"
#include <QDesktopWidget>
#include <QApplication>
#include <QMouseEvent>
#include <QFile>
#include <QIcon>

MagnifyTimerWidget::MagnifyTimerWidget(QWidget *parent)
    : QDialog(parent)
    , m_iSumTime(0)
    , m_iTime(0)
    , m_iMinute(0)
    , m_iSecond(0)
{
    InitUi();

    InitProperty();
}

MagnifyTimerWidget::~MagnifyTimerWidget()
{

}

void MagnifyTimerWidget::SetState(MagnifyTimerWidget::STATES state)
{
    m_yState = state;
}

void MagnifyTimerWidget::InitUi()
{
    m_pRootLayout = new QHBoxLayout(this);
    m_pRootLayout->setContentsMargins(0,0,0,0);
    m_pRootLayout->setSpacing(0);

    m_pRootWidget = new QWidget(this);
    m_pRootWidget->setObjectName("RootWidget");
    m_pRootWidgetLayout = new QHBoxLayout(m_pRootWidget);
    m_pRootWidgetLayout->setContentsMargins(0,0,0,0);
    m_pRootWidgetLayout->setSpacing(0);
    m_pRootWidgetLayout->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    m_pTimeWidget = new QWidget(m_pRootWidget);
    m_pTimeWidget->setObjectName("TimeWidget");
    m_pTimeWidgetLayout = new QHBoxLayout(m_pTimeWidget);
    m_pTimeWidgetLayout->setContentsMargins(0,0,0,0);
    m_pTimeWidgetLayout->setSpacing(0);

    m_pTimeLabel = new QLabel(m_pTimeWidget);
    m_pTimeLabel->setObjectName("TimeLabel");
    m_pTimeLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pTimeLabel->setText("00");
    m_pColon_0 = new QLabel(m_pTimeWidget);
    m_pColon_0->setObjectName("Colon");
    m_pColon_0->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pColon_0->setText(":");
    m_pMinuteLabel = new QLabel(m_pTimeWidget);
    m_pMinuteLabel->setObjectName("MinuteLabel");
    m_pMinuteLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pMinuteLabel->setText("00");
    m_pColon_1 = new QLabel(m_pTimeWidget);
    m_pColon_1->setObjectName("Colon");
    m_pColon_1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pColon_1->setText(":");
    m_pSecondLabel = new QLabel(m_pTimeWidget);
    m_pSecondLabel->setObjectName("SecondLabel");
    m_pSecondLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pSecondLabel->setText("00");

    m_pTimeWidgetLayout->addWidget(m_pTimeLabel);
    m_pTimeWidgetLayout->addWidget(m_pColon_0);
    m_pTimeWidgetLayout->addWidget(m_pMinuteLabel);
    m_pTimeWidgetLayout->addWidget(m_pColon_1);
    m_pTimeWidgetLayout->addWidget(m_pSecondLabel);

    m_pRootWidgetLayout->addWidget(m_pTimeWidget);

    m_pRootLayout->addWidget(m_pRootWidget);
}

void MagnifyTimerWidget::InitProperty()
{
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background,Qt::transparent);
    setPalette(pal);
    setWindowOpacity(1);
    setAttribute(Qt::WA_TranslucentBackground);

    this->setWindowIcon(QIcon(":/icon/timer.ico"));

    QFile resourceqss(":/qss/MagnifyTimerWidget.qss");
    resourceqss.open(QFile::ReadOnly);
    this->setStyleSheet(resourceqss.readAll());
    resourceqss.close();

    m_yState = COUNTDOWN;
}

void MagnifyTimerWidget::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)
    QDesktopWidget* desktopWidget = QApplication::desktop();
    //获取可用桌面大小
    QRect deskRect = desktopWidget->availableGeometry();
    //获取设备屏幕大小
    QRect screenRect = desktopWidget->screenGeometry();

    int g_nActScreenX = screenRect.width();
    int g_nActScreenY = screenRect.height();

    if(m_yState == MagnifyTimerWidget::STATES::COUNTDOWN)
    {
        m_pTimeLabel->setVisible(false);
        m_pColon_0->setVisible(false);
    } else if(m_yState == MagnifyTimerWidget::STATES::THETIMER)
    {
        m_pTimeLabel->setVisible(true);
        m_pColon_0->setVisible(true);
    }

    this->resize(g_nActScreenX, g_nActScreenY);
    //this->show();
}

void MagnifyTimerWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        emit closeed();
    }
}

void MagnifyTimerWidget::updatetimer(int timer)
{
    m_iTime = timer/3600;
    m_iMinute = timer%3600/60;
    m_iSecond = timer%3600%60;

    m_pTimeLabel->setText(QString::number(m_iTime/10) + QString::number(m_iTime%10));
    m_pMinuteLabel->setText(QString::number(m_iMinute/10) + QString::number(m_iMinute%10));
    m_pSecondLabel->setText(QString::number(m_iSecond/10) + QString::number(m_iSecond%10));
}
